using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Security;
using System.Security.Permissions;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Collections.ObjectModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;

namespace AJAX.Config
{
    /// <summary>
    /// A Feature Reciever for SharePoint.OrcasConfig
    /// Original sample  : http://www.codeplex.com/sharepointajax
    /// (Daniel Larson & Tony Bierman) 
    /// </summary>
    public class FeatureReceiver : WebConfigEngine
    {
        /// <summary>
        /// Gets the owner modif.
        /// </summary>
        /// <value>The owner modif.</value>
        override protected string OwnerModif
        {
            get { return "AJAX.Config"; }
        }


        /// <summary>
        /// Adds the Orcas configuration to the web.config. 
        /// </summary>
        /// <param name="app"></param>
        /// <param name="removeModification"></param>
        /// <remarks>
        /// Tip o' the hat to Tony Bierman, 
        /// http://sharepointsolutions.blogspot.com/2006/12/using-spwebconfigmodificat_116736917110571614.html
        /// </remarks>
        protected override void AddConfigurationToWebConfig(SPWebApplication app)
        {    
            #region AJAX 1.0

            #region 1. Add a <sectionGroup>element to the <configSections>tag:
            //<configSections>    
            //    <sectionGroup name=""system.web.extensions"" type=""System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"">
            //      <sectionGroup name=""scripting"" type=""System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"">
            //        <section name=""scriptResourceHandler"" type=""System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" requirePermission=""false"" allowDefinition=""MachineToApplication""/>
            //        <sectionGroup name=""webServices"" type=""System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"">
            //          <section name=""jsonSerialization"" type=""System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" requirePermission=""false"" allowDefinition=""Everywhere"" />
            //          <section name=""profileService"" type=""System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" requirePermission=""false"" allowDefinition=""MachineToApplication"" />
            //          <section name=""authenticationService"" type=""System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" requirePermission=""false"" allowDefinition=""MachineToApplication"" />
            //        </sectionGroup>
            //      </sectionGroup>
            //    </sectionGroup>
            //</configSections> 

            #region Add new section sectionGroup

            AddNodeValue(
                "sectionGroup[@name='system.web.extensions']",
                "configuration/configSections",
                @"<sectionGroup name=""system.web.extensions"" type=""System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""></sectionGroup>"
                );

            SaveWebConfig(app);
            #endregion

            #region Add new section sectionGroup
            AddNodeValue(
                "sectionGroup[@name='scripting']",
                "configuration/configSections/sectionGroup[@name='system.web.extensions']",
                @"<sectionGroup name=""scripting"" type=""System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""></sectionGroup>"
                );

            SaveWebConfig(app);
            #endregion

            #region Add new section section /sectionGroup
            //Add new section section
            AddNodeValue(
                "section[@name='scriptResourceHandler']",
                "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']",
                @"<section name=""scriptResourceHandler"" type=""System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" requirePermission=""false"" allowDefinition=""MachineToApplication""/>"
                );

            //Add new section sectionGroup
            AddNodeValue(
                "sectionGroup[@name='webServices']",
                "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']",
                @"<sectionGroup name=""webServices"" type=""System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""></sectionGroup>"
                );

            SaveWebConfig(app);
            #endregion

            #region Add new section sub section
            //Ad new section 
            AddNodeValue(
                "section[@name='jsonSerialization']",
                "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/sectionGroup[@name='webServices']",
                @"<section name=""jsonSerialization"" type=""System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" requirePermission=""false"" allowDefinition=""Everywhere"" />"
                );

            AddNodeValue(
                "section[@name='profileService']",
                "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/sectionGroup[@name='webServices']",
                @"<section name=""profileService"" type=""System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" requirePermission=""false"" allowDefinition=""MachineToApplication"" />"
                );

            AddNodeValue(
                "section[@name='authenticationService']",
                "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/sectionGroup[@name='webServices']",
                @"<section name=""authenticationService"" type=""System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" requirePermission=""false"" allowDefinition=""MachineToApplication"" />"
                );


            SaveWebConfig(app);

            #endregion
            #endregion

            #region 2. Add a <controls> section as a child of the <system.web>/<pages> tag.
            //<pages>
            //  <controls>
            //    <add tagPrefix=""asp"" namespace=""System.Web.UI"" assembly=""System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>
            //  </controls>
            //</pages>              
                   
            //Fix from CODEPLEX
            //==============================
          
            //AddNodeValue("pages",
            //"configuration/system.web",
            //@"<pages />"
            //);

            //==> copy from my own server
            //<pages enableSessionState=""false"" enableViewState=""true"" enableViewStateMac=""true"" validateRequest=""false"" pageParserFilterType=""Microsoft.SharePoint.ApplicationRuntime.SPPageParserFilter, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"" asyncTimeout=""7"">
            
            AddSection(
            "pages",
            "configuration/system.web"
            );


            SaveWebConfig(app);

            AddSection("controls",
            "configuration/system.web/pages" );

            SaveWebConfig(app);

            AddNodeValue(
            "add[@tagPrefix='asp']",
            "configuration/system.web/pages/controls",
            @"<add tagPrefix=""asp"" namespace=""System.Web.UI"" assembly=""System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>"
            );

            SaveWebConfig(app);
            #endregion

            #region 3. Add the following tag to the <assemblies> tag, within <compilation>.
            //<assemblies> 
            // <add assembly=""System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>
            //</assemblies> 

            AddNodeValue(
            "add[@assembly='System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']",
            "configuration/system.web/compilation/assemblies",
            @"<add assembly=""System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>"
            );

            SaveWebConfig(app);
            #endregion

            #region 4. Add some new registrations to the end of the <httpHandlers> section:
            //<httpHandlers> 
            //     <add verb=""*"" path=""*.asmx"" validate=""false"" type=""System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>
            //     <add verb=""*"" path=""*_AppService.axd"" validate=""false"" type=""System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>
            //     <add verb=""GET,HEAD"" path=""ScriptResource.axd"" type=""System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" validate=""false""/>
            // </httpHandlers> 

            AddNodeValue(
            "add[@path='*.asmx']",
            "configuration/system.web/httpHandlers",
            @"<add verb=""*"" path=""*.asmx"" validate=""false"" type=""System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>"
            );

            AddNodeValue(
            "add[@path='*_AppService.axd']",
            "configuration/system.web/httpHandlers",
            @"<add verb=""*"" path=""*_AppService.axd"" validate=""false"" type=""System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>"
            );

            AddNodeValue(
            "add[@path='ScriptResource.axd']",
            "configuration/system.web/httpHandlers",
            @"<add verb=""GET,HEAD"" path=""ScriptResource.axd"" type=""System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" validate=""false""/>"
            );

            SaveWebConfig(app);

            #endregion

            #region 5. Add a new registration to the HttpModules section, beneath any existing registrations.
            //<httpModules> 
            //    <add name=""ScriptModule"" type=""System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>
            //</httpModules> 

            AddNodeValue(
            "add[@name='ScriptModule']",
            "configuration/system.web/httpModules",
            @"<add name=""ScriptModule"" type=""System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>"
            );

            SaveWebConfig(app);

            #endregion

            #region 6. Add a SafeControl entry for the System.Web.UI namespace from Microsoft Ajax Extensions, within the <SharePoint>/<SafeControls>section:
            //<SafeControls> 
            //    <SafeControl Assembly=""System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" Namespace=""System.Web.UI"" TypeName=""*"" Safe=""True"" />
            //</SafeControls> 

  //tip form Jan in case of
  //          AddNodeValue(
  //"SafeControl[@Assembly=\"System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"][@Namespace=\"System.Web.UI\"][@TypeName=\"*\"]",
  //"configuration/SharePoint/SafeControls",
  //@"<SafeControl Assembly=""System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" Namespace=""System.Web.UI"" TypeName=""*"" Safe=""True"" />"); 


            AddNodeValue(
            "SafeControl[@Assembly='System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'][@Namespace='System.Web.UI'][@TypeName='*']",
            "configuration/SharePoint/SafeControls",
            @"<SafeControl Assembly=""System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" Namespace=""System.Web.UI"" TypeName=""*"" Safe=""True"" />"
            );


            SaveWebConfig(app);

            #endregion

            #region 7. Finally, add the following configuration tags at the bottom of web.config, near the bottom before the end <configuration> tag.

            //<system.web.extensions>
            //  <scripting>
            //    <webServices>
            //    </webServices>
            //  </scripting>
            //</system.web.extensions>
            //<system.webServer>
            //  <validation validateIntegratedModeConfiguration=""false""/>
            //  <modules>
            //    <add name=""ScriptModule"" preCondition=""integratedMode"" type=""System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>
            //  </modules>
            //  <handlers>
            //    <remove name=""WebServiceHandlerFactory-Integrated"" />
            //    <add name=""ScriptHandlerFactory"" verb=""*"" path=""*.asmx"" preCondition=""integratedMode""
            //         type=""System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>
            //    <add name=""ScriptHandlerFactoryAppServices"" verb=""*"" path=""*_AppService.axd"" preCondition=""integratedMode"" type=""System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>
            //    <add name=""ScriptResource"" preCondition=""integratedMode"" verb=""GET,HEAD"" path=""ScriptResource.axd"" type=""System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" />
            //  </handlers>
            //</system.webServer>  

            #region 1 - Sections
            AddNodeValue(
            "system.web.extensions",
            "configuration",
            @"<system.web.extensions />"
            );

            SaveWebConfig(app);

            AddNodeValue(
            "scripting",
            "configuration/system.web.extensions",
            @"<scripting />"
            );

            SaveWebConfig(app);

            AddNodeValue(
            "webServices",
            "configuration/system.web.extensions/scripting",
            @"<webServices />"
            );

            SaveWebConfig(app);

            AddNodeValue("system.webServer",
           "configuration",
           @"<system.webServer />"
           );

            AddNodeValue("modules",
           "configuration/system.webServer",
           @"<modules />"
           );

            AddNodeValue("handlers",
           "configuration/system.webServer",
           @"<handlers />"
           );

            SaveWebConfig(app);

            #endregion

            #region 2 - Nodes

            /////////////////////////////
            //Add sub nodes with values
            /////////////////////////////

            AddNodeValue(
                "validation[@validateIntegratedModeConfiguration='false']",
                "configuration/system.webServer",
                @"<validation validateIntegratedModeConfiguration=""false""/>"
                );

            AddNodeValue(
                "add[@name='ScriptModule']",
                "configuration/system.webServer/modules",
                @"<add name=""ScriptModule"" preCondition=""integratedMode"" type=""System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>"
                );

            AddNodeValue(
                "remove[@name='WebServiceHandlerFactory-Integrated']",
                "configuration/system.webServer/handlers",
                @"<remove name=""WebServiceHandlerFactory-Integrated"" />"
                );

            AddNodeValue(
                "add[@name='ScriptHandlerFactory']",
                "configuration/system.webServer/handlers",
                @"<add name=""ScriptHandlerFactory"" verb=""*"" path=""*.asmx"" preCondition=""integratedMode"" type=""System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>"
                );


            AddNodeValue(
               "add[@name='ScriptHandlerFactoryAppServices']",
               "configuration/system.webServer/handlers",
               @"<add name=""ScriptHandlerFactoryAppServices"" verb=""*"" path=""*_AppService.axd"" preCondition=""integratedMode"" type=""System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""/>"
               );

            AddNodeValue(
                "add[@name='ScriptResource']",
                "configuration/system.webServer/handlers",
                @"<add name=""ScriptResource"" preCondition=""integratedMode"" verb=""GET,HEAD"" path=""ScriptResource.axd"" type=""System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" />"
                );

            SaveWebConfig(app);

            #endregion

            #endregion

            #endregion
        }
    } // FeatureReceiver
} // AJAX.Config